//
//  ZBLoginViewModel.m
//  BaseRACDemo
//
//  Created by 庄波 on 2018/3/27.
//  Copyright © 2018年 庄波. All rights reserved.
//

#import "ZBLoginViewModel.h"
#import "ZBHTTPService+Login.h"
#import "MHHTTPResponse.h"
#import "ZBUserOwn.h"
typedef void(^Completed)();

@interface ZBLoginViewModel ()
{
    BOOL isUserFace;
}
@property (nonatomic, assign) NSInteger second;
@property (nonatomic, strong) RACDisposable *dispoable;
@end
@implementation ZBLoginViewModel

-(instancetype)initWithServices:(id<ZBViewModelServices>)services params:(NSDictionary *)params{
    self = [super initWithServices:services params:params];
    if (self) {
        //初始化
        self.timerSubject = [RACSubject subject];
        isUserFace = NO;
    }
    return self;
}

-(void)initialize{
    self.title = @"登录页面";
    //隐藏 登录界面导航栏
    self.prefersNavigationBarHidden = YES;
    
    self.isValidLogin = [RACObserve(self, phoneStr) map:^id _Nullable(NSString *value) {
        return @([value isMobileNumber]);
    }];
    
    [self.isValidLogin subscribeNext:^(id  _Nullable x) {
        isUserFace = [x boolValue];
    }];
                         
    
    //获取验证码
    self.btnGetCodeCommand = [[RACCommand alloc] initWithEnabled:nil signalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        //请求
        [self singalTimer];
        RACSignal *singleCode = [self.services.client getCodeWithPhone:self.phoneStr];
        [singleCode subscribeNext:^(id  _Nullable x) {
            MHHTTPResponse*requset = x;
            if (requset.code == MHHTTPResponseCodeSuccess) {
                [CustomMBHUD showSuccessWithStatus:@"获取成功"];
            }else{
                [CustomMBHUD showErrorWithStatus:requset.msg];
            }
        }];
        
        [singleCode subscribeError:^(NSError * _Nullable error) {
            [CustomMBHUD showErrorWithStatus:error.description];
        }];
        return singleCode;
    }];
    
    //登陆
    self.btnLoginCommand = [[RACCommand alloc] initWithEnabled:nil signalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        NSArray *aryData = [NSArray arrayWithArray:input];
        RACSignal *singleCode = [self.services.client loginWithPhone:aryData[0] withCode:aryData[1]];
        [singleCode subscribeNext:^(id  _Nullable x) {
            //登陆
            ZBUserOwn *info = x;
            [self.services.client loginUser:info];
        }];
        
        [singleCode subscribeError:^(NSError * _Nullable error) {
            [CustomMBHUD showErrorWithStatus:@"登陆失败"];
        }];
        return singleCode;
    }];

}

//倒计时
- (void)singalTimer{
    self.second = 60;
    self.isValidLogin = [RACSignal return:@0];
    RACSignal *single = [[[[RACSignal interval:1 onScheduler:[RACScheduler mainThreadScheduler]] startWith:[NSDate date]]
                          map:^id _Nullable(NSDate * _Nullable value) {
                              NSString *text = [NSString stringWithFormat:@"%.0ld秒",(long)self.second];
                              if (self.second == 0) {
                                  [self.dispoable dispose];
                                  [self.timerSubject sendNext:@"获取验证码"];
                              }
                              self.second --;
                              return text;
                          }]takeUntil:self.rac_willDeallocSignal];
    
    
    self.dispoable  = [single subscribeNext:^(NSDate * _Nullable x) {
                                [self.timerSubject sendNext:x];
                            }];
}

@end
